package com.feicheng.lanniao.service.impl;

import com.feicheng.lanniao.entity.House;
import com.feicheng.lanniao.entity.SysRegion;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.HouseMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.lanniao.service.IHouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements IHouseService {
	//新增房子信息
	@Override
	public ResultInfo addHouse(House house) {
		if (null == house.getDes()) {
			throw new SystemException("描述不能为空，请输入一下");
		}
		if (null == house.getProvienceId()) {
			throw new SystemException("省不能为空，请输入一下");
		}
		if (null == house.getCityId()) {
			throw new SystemException("市不能为空，请输入一下");
		}
		if (null == house.getReignId()) {
			throw new SystemException("区不能为空，请输入一下");
		}
		if (null == house.getArea()) {
			throw new SystemException("面积不能为空，请输入一下");
		}
		house.setSaleFlg(true);
		this.save(house);
		return ResultInfo.success();
	}
	//房屋基本信息的分页查询+模糊查询
	@Override
	public ResultInfo getHousePage(String des, 
			Integer provienceId, Integer cityId, Integer reignId, 
			Integer currentPage, Integer pageSize) {
		QueryWrapper<House> houseWrapper = new QueryWrapper<House>();
		if (StringUtils.hasLength(des)) {
			houseWrapper.like("des", des);
		}
		houseWrapper.eq("sale_flg", "1");
		System.out.println(this.list(houseWrapper));
		Page<House> pageInfo = new Page<House>(currentPage, pageSize);
		System.out.println(ResultInfo.success(this.page(pageInfo, houseWrapper)));
		return ResultInfo.success(this.page(pageInfo, houseWrapper));
	}
	
	 //根据houseId 查询，之后修改其SaleFlg的值为0，达到假删除效果
	@Override
	public ResultInfo deleteHouse(House house) {
		if (this.getById(house.getHouseId()) == null) {
			throw new SystemException("请输入要删除的房子id");
		}
		house.setSaleFlg(false);
		this.updateById(house);
		return ResultInfo.success();
	}
	
	//详情页的修改：接到1个house对象做修改
	@Override
	public ResultInfo updateHouse(House house) {
		if (null == house.getHouseId()) {
			throw new SystemException("请输入要更新房子的id");
		}
		this.updateById(house);
		return ResultInfo.success();
	}
	
	//详情页
	@Override
	public ResultInfo findDetails(Integer houseId) {
		System.out.println(this.getById(houseId).toString());
		return ResultInfo.success(this.getById(houseId));
	}
}
